
#include "../common/error.h"
#include "../common/sparse_matrix.h"
#include "../common/partition_group.h"
#include "../common/cluster.h"
#include "../common/mem.h"

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
	/* Variables */
	const char* adjacency_mat_file;
	sparse_matrix_arr* original_matrix;
	elem precision;
	partitions* P;
	double Que;

	/*  Make sure we have the correct number of arguments, if not - exit "nicely" */
	if (argc != 3)
		nice_exit("Usage: %s <adjacency-mat-file> <precision>\n\n", argv[0]);
	
	/*  Grab arguments */
	adjacency_mat_file = argv[1];
	precision = (elem) atof(argv[2]);

	/*  Validate precision parameter */
	if (precision <= 0.0)
		fatal_error("Bad precision value. Should be above 0.");

	debug_output("Reading matrix... ");

	/*  Read adjacency matrix (exits with an error if cannot read the file) */
	original_matrix = sparse_matrix__read_from_file(adjacency_mat_file, -1);

	debug_output("Done.\n");

	debug_output("Clustering... \n");

	/*  Cluster the network */
	P = partition_cluster(original_matrix, precision, &Que);
	
	debug_output("Done Clustering.\n");

	/*  Print clusters (partitions) */
	printf("%f %i\n", Que, PtrLinkedList__Count(P->non_divisible_groups));
	print_partitions(P);

	/*  Deallocate everything */
	free_all();

	return 0;
}